package top.lyc829.mymusic.model.repository

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import top.lyc829.mymusic.model.db.BannerDao
import top.lyc829.mymusic.model.http.AppException
import top.lyc829.mymusic.model.http.MyNetwork
import top.lyc829.mymusic.util.Const
import java.lang.RuntimeException

/**
 * @Author lyc
 * @create 2021-07-17 23:17
 */
class PlayListRepository (private val network : MyNetwork) {

    suspend fun getPlayListDetail(id: Long) = withContext(Dispatchers.IO) {
        val playList = network.getPlayListDetail(id)
        if (playList.code != Const.SUCCESS) {
            throw AppException(playList.code,"获取歌单详情失败")
        }
        playList.playlist
    }

    suspend fun getTopPlayList(offset: Int = 0, limit: Int = 50, order: String = Const.HOT, cat: String = Const.CATEGORY_ALL) = withContext(Dispatchers.IO) {
        val playList = network.getTopPlayList(offset, limit, order, cat)
        if (playList.code != Const.SUCCESS) {
            throw AppException(playList.code, "获取歌单详情失败")
        }
        playList.playList
    }


    companion object {
        private var instance: PlayListRepository? = null
        fun getInstance(network: MyNetwork): PlayListRepository {
            if (instance == null) {
                synchronized(PlayListRepository::class.java) {
                    if (instance == null) {
                        instance = PlayListRepository(network)
                    }
                }
            }
            return instance!!
        }
    }
}